/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Save File to.
 *
 * The Initial Developer of the Original Code is
 * Atte Kemppilä.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */


const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

const Application = Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication);
const compareVersions = Cc["@mozilla.org/xpcom/version-comparator;1"].getService(Ci.nsIVersionComparator).compare;
const obsSvc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
const prefSvc = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);


function Service()
{
    Cu.import("resource://savefileto/constants.jsm");
    
    this.wrappedJSObject = this;
}

Service.prototype =
{
    classDescription: "Save File to service",
    classID: Components.ID("{9e7881e3-bebe-4e10-a1c4-efb9236d9467}"),
    contractID: "@savefileto.mozdev.org/service;1",
    
    _xpcom_categories: [{category: "app-startup", service: true}],
    
    QueryInterface: XPCOMUtils.generateQI([
        Ci.nsIObserver,
        Ci.nsISupports,
        Ci.nsISupportsWeakReference]),
    
    // nsIObserver
    observe: function(subject, topic, data)
    {
        switch (topic)
        {
            case "app-startup":
                obsSvc.addObserver(this, "profile-after-change", true);
                obsSvc.addObserver(this, "private-browsing", true);
                obsSvc.addObserver(this, "quit-application", true);
                break;
                
            case "profile-after-change":
                this.upgrade();
                break;
                
            case "private-browsing":
                if (data == "enter")
                    this.onEnterPrivateBrowsing();
                else if (data == "exit")
                    this.onExitPrivateBrowsing();
                break;
                
            case "quit-application":
                this.cleanUp();
                break;
        }
    },
    
    cleanUp: function(force)
    {
        var prefs = prefSvc.getBranch(EXT_PREF_BRANCH);
        if (! (force || prefs.getBoolPref("clearRecent")))
            return;
        
        for each (var type in TYPES)
            try
            {
                prefs.clearUserPref(type + ".recent");
            }
            catch (e) { /* ingore */ }
    },
    
    upgrade: function()
    {
        var version = Application.extensions.get(EXT_ID).version;
        var lastVersion = prefSvc.getBranch(EXT_PREF_BRANCH).getCharPref("lastVersion");
        
        if (compareVersions(version, lastVersion) == 0)
            return;
        
        try
        {
            Application.console.log("Save File to: upgrading to " + version);
            
            var module = {};
            Cu.import("resource://savefileto/upgrade.jsm", module);
            module.upgrade(version);
        }
        catch (e)
        {
            Cu.reportError(e);
        }
    },
    
    onEnterPrivateBrowsing: function()
    {
        var prefs = prefSvc.getBranch(EXT_PREF_BRANCH);
        for each (var type in TYPES)
        {
            var value = prefs.getCharPref(type + ".recent");
            prefs.setCharPref(type + ".recent.backup", value);
        }
    },
    
    onExitPrivateBrowsing: function()
    {
        var prefs = prefSvc.getBranch(EXT_PREF_BRANCH);
        for each (var type in TYPES)
        {
            var value = prefs.getCharPref(type + ".recent.backup");
            prefs.clearUserPref(type + ".recent.backup");
            prefs.setCharPref(type + ".recent", value);
        }
    }
}


function NSGetModule(compMgr, fileSpec)
{
    return XPCOMUtils.generateModule([Service]);
}
